package com.wannengbang.cloudleader.homepage;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.google.gson.Gson;
import com.wannengbang.cloudleader.R;
import com.wannengbang.cloudleader.base.BaseActivity;
import com.wannengbang.cloudleader.base.DataCallBack;
import com.wannengbang.cloudleader.bean.BaseResponseBean;
import com.wannengbang.cloudleader.bean.UserInfoBean;
import com.wannengbang.cloudleader.homepage.model.HomePageModelImpl;
import com.wannengbang.cloudleader.homepage.model.IHomePageModel;
import com.wannengbang.cloudleader.login.model.ILoginModel;
import com.wannengbang.cloudleader.login.model.LoginModelImpl;
import com.wannengbang.cloudleader.mine.UserAgreementActivity;
import com.wannengbang.cloudleader.mine.model.MineModelImpl;
import com.wannengbang.cloudleader.utils.CashierInputFilter;
import com.wannengbang.cloudleader.utils.FastClickUtils;
import com.wannengbang.cloudleader.utils.NumberFormatUtils;
import com.wannengbang.cloudleader.utils.ToastUtil;
import com.wannengbang.cloudleader.widget.AppTitleBar;
import com.wannengbang.cloudleader.widget.CountDownTimerButton4;
import com.wannengbang.cloudleader.widget.ViewLoading;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class WithdrawActivity extends BaseActivity {

    @BindView(R.id.title_bar)
    AppTitleBar titleBar;
    @BindView(R.id.tv_withdraw_money_1)
    TextView tvWithdrawMoney1;
    @BindView(R.id.tv_withdraw_money_2)
    TextView tvWithdrawMoney2;
    @BindView(R.id.ll_withdraw_type)
    LinearLayout llWithdrawType;
    @BindView(R.id.edit_money)
    EditText editMoney;
    @BindView(R.id.cb_remember)
    CheckBox cbRemember;
    @BindView(R.id.tv_withdraw_agree)
    TextView tvWithdrawAgree;
    @BindView(R.id.tv_commit)
    TextView tvCommit;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_mobile)
    TextView tvMobile;
    @BindView(R.id.edit_code)
    EditText editCode;
    @BindView(R.id.tv_send_code)
    CountDownTimerButton4 tvSendCode;
    @BindView(R.id.tv_account)
    TextView tvAccount;
    @BindView(R.id.ll_account_type)
    LinearLayout llAccountType;
    @BindView(R.id.tv_fx_un_withdrawal_money)
    TextView tvFxUnWithdrawalMoney;
    @BindView(R.id.tv_withdrawal_envelope)
    TextView tv_withdrawal_envelope;
    private ArrayList<String> options1Items = new ArrayList<>();
    private ArrayList<String> options2Items = new ArrayList<>();
    private IHomePageModel homePageModel;
    private ILoginModel loginModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_withdraw);
        ButterKnife.bind(this);
        initView();
        requestUserInfo();
    }


    public void initView() {
        loginModel = new LoginModelImpl();
        homePageModel = new HomePageModelImpl();
        InputFilter[] filters = {new CashierInputFilter()};
        editMoney.setFilters(filters);
        options1Items = new ArrayList<>();
        options1Items.add("分润");
        options1Items.add("返现");
        options1Items.add("红包");

        options2Items.add("银行卡账户");
        options2Items.add("支付宝账户");

        titleBar.setRightClickListener(() -> {
            showActivity(WithdrawListActivity.class);
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        requestUserInfo();
    }

    public void requestUserInfo() {
        new MineModelImpl().requestMine(new DataCallBack<UserInfoBean>() {
            @Override
            public void onSuccessful(UserInfoBean bean) {
                Log.e("红包可提现",new Gson().toJson(bean));
                String can_withdrawal_money = NumberFormatUtils.formatTwoDecimal(Double.parseDouble(bean.getData().getCan_withdrawal_money()) / 100);
                String fx_can_withdrawal_money = NumberFormatUtils.formatTwoDecimal(Double.parseDouble(bean.getData().getFx_can_withdrawal_money()) / 100);
                String fx_un_withdrawal_money = NumberFormatUtils.formatTwoDecimal(Double.parseDouble(bean.getData().getFx_un_withdrawal_money()) / 100);
                String hb_can_withdrawal_money = NumberFormatUtils.formatTwoDecimal(Double.parseDouble(bean.getData().getHb_can_withdrawal_money()) / 100);
                tvWithdrawMoney1.setText(can_withdrawal_money);
                tvWithdrawMoney2.setText(fx_can_withdrawal_money);
                tvMobile.setText(bean.getData().getMobile());
                tvFxUnWithdrawalMoney.setText(fx_un_withdrawal_money);
                tv_withdrawal_envelope.setText(hb_can_withdrawal_money);
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    private void showPickerView() {// 弹出选择器

        OptionsPickerView pvOptions = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                tvName.setText(options1Items.get(options1));
            }
        }).setTitleText("选择提现类型")
                .setTitleSize(16)
                .setSubmitColor(getResources().getColor(R.color.text_color_2))//确定按钮文字颜色
                .setSubCalSize(16)
                .setCancelColor(getResources().getColor(R.color.text_color_2))//取消按钮文字颜色
                .setTitleBgColor(getResources().getColor(R.color.white))
                .setDividerColor(getResources().getColor(R.color.line_color_gray))
                .setTextColorCenter(getResources().getColor(R.color.text_color_2))
                .setContentTextSize(18)
                .setLineSpacingMultiplier(2)
                .isDialog(true)
                .build();

        pvOptions.setPicker(options1Items);//三级选择器

        Dialog mDialog = pvOptions.getDialog();
        if (mDialog != null) {
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    Gravity.BOTTOM);

            params.leftMargin = 0;
            params.rightMargin = 0;
            pvOptions.getDialogContainerLayout().setLayoutParams(params);

            Window window = mDialog.getWindow();
            if (window != null) {
                window.setWindowAnimations(R.style.picker_view_slide_anim);//修改动画样式
                window.getDecorView().setPadding(0, 0, 0, 0);
                WindowManager.LayoutParams lp = window.getAttributes();
                lp.width = WindowManager.LayoutParams.MATCH_PARENT;
                lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
                lp.gravity = Gravity.BOTTOM;
            }
        }
        pvOptions.show();

    }

    private void showAccountPickerView() {// 弹出选择器

        OptionsPickerView pvOptions = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                tvAccount.setText(options2Items.get(options1));
            }
        }).setTitleText("选择到账方式")
                .setTitleSize(16)
                .setSubmitColor(getResources().getColor(R.color.text_color_2))//确定按钮文字颜色
                .setSubCalSize(16)
                .setCancelColor(getResources().getColor(R.color.text_color_2))//取消按钮文字颜色
                .setTitleBgColor(getResources().getColor(R.color.white))
                .setDividerColor(getResources().getColor(R.color.line_color_gray))
                .setTextColorCenter(getResources().getColor(R.color.text_color_2))
                .setContentTextSize(18)
                .setLineSpacingMultiplier(2)
                .isDialog(true)
                .build();

        pvOptions.setPicker(options2Items);//三级选择器

        Dialog mDialog = pvOptions.getDialog();
        if (mDialog != null) {
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    Gravity.BOTTOM);

            params.leftMargin = 0;
            params.rightMargin = 0;
            pvOptions.getDialogContainerLayout().setLayoutParams(params);

            Window window = mDialog.getWindow();
            if (window != null) {
                window.setWindowAnimations(R.style.picker_view_slide_anim);//修改动画样式
                window.getDecorView().setPadding(0, 0, 0, 0);
                WindowManager.LayoutParams lp = window.getAttributes();
                lp.width = WindowManager.LayoutParams.MATCH_PARENT;
                lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
                lp.gravity = Gravity.BOTTOM;
            }
        }
        pvOptions.show();

    }

    public void withdraw(String code, String money, String type, String with_type) {
        ViewLoading.showProgress(mActivity);
        homePageModel.requestAgentWithdrawalAdd(code, money, type, with_type, new DataCallBack<BaseResponseBean>() {
            @Override
            public void onSuccessful(BaseResponseBean bean) {
                ToastUtil.showShort(bean.getMsg());
                showActivity(WithdrawListActivity.class);
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    public void sendCode() {
        String mobile = tvMobile.getText().toString();
        loginModel.requestSendVerifyCode(mobile, "2", new DataCallBack<BaseResponseBean>() {
            @Override
            public void onSuccessful(BaseResponseBean bean) {
                ToastUtil.showShort(bean.getMsg());
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    @OnClick({R.id.ll_withdraw_type, R.id.ll_account_type, R.id.tv_withdraw_agree, R.id.tv_commit, R.id.tv_send_code,})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_withdraw_type:
                showPickerView();
                break;
            case R.id.ll_account_type:
                showAccountPickerView();
                break;
            case R.id.tv_withdraw_agree:
                Intent intent = new Intent(mActivity, UserAgreementActivity.class);
                intent.putExtra("type", 3);
                startActivity(intent);
                break;
            case R.id.tv_commit:
                String type = tvName.getText().toString();
                if (TextUtils.isEmpty(type)) {
                    ToastUtil.showShort("请选择提现类型");
                    return;
                }
//                String account = tvAccount.getText().toString();
//                if (TextUtils.isEmpty(account)) {
//                    ToastUtil.showShort("请选择到账方式");
//                    return;
//                }
                String money = editMoney.getText().toString();
                if (TextUtils.isEmpty(money)) {
                    ToastUtil.showShort("请输入提现金额");
                    return;
                }
                String code = editCode.getText().toString();
                if (TextUtils.isEmpty(code)) {
                    ToastUtil.showShort("请输入验证码");
                    return;
                }
                if (!cbRemember.isChecked()) {
                    ToastUtil.showShort("请同意提现协议");
                    return;
                }

                if (FastClickUtils.isFastClick()) {
                    return;
                }
                //钱数 单位分
                String moneyUnit = String.valueOf(Double.parseDouble(money) * 100);
                moneyUnit = moneyUnit.substring(0, moneyUnit.indexOf("."));

                //0 分润  1 返现 2红包
                String withdraw_type = "0";
                if ("分润".equals(type)) {
                    withdraw_type = "0";
                } else if ("返现".equals(type)){
                    withdraw_type = "1";
                }else {
                    withdraw_type = "2";
                }
                //with_type 1银行卡 2支付宝
                String with_type = "1";
//                if ("银行卡账户".equals(account)) {
//                    with_type = "1";
//                } else {
//                    with_type = "2";
//                }
                withdraw(code, moneyUnit, withdraw_type, with_type);
                break;
            case R.id.tv_send_code:
                sendCode();
                break;
        }
    }
}
